package cn.airfei.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * @description: 初始化器，channel注册后会执行里面相应的初始化方法
 * @author: air
 * @create: 2019-10-17 16:26
 **/
public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 通过SocketChannel去获得相应管道
        ChannelPipeline channelPipeline = channel.pipeline();

        // 通过管道添加Handler
        // HttpServerCodec是有netty自己提供的助手类，可以理解为拦截器
        // 当请求到服务器，我们需要做解码，响应到客户端做编码
        channelPipeline.addLast("HttpServerCodec", new HttpServerCodec());


        // 添加自定义的助手类， 返回 hello netty
        channelPipeline.addLast("customHandler", new CustomHandler());


    }
}
